package com.csy.property.community.service.impl;

import com.csy.commons.core.utils.OrikaUtils;
import com.csy.property.community.domain.AjCommunity;
import com.csy.property.community.domain.dto.AjCommunityDto;
import com.csy.property.community.domain.vo.AjCommunityVo;
import com.csy.property.community.mapper.AjCommunityMapper;
import com.csy.property.community.service.AjCommunityService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: sunxx
 * @Date: 2023/12/31 - 12 - 31 - 17:48
 * @Description: com.sxx.ajcommunity.community.service.impl
 * @version: 1.0
 */
@Service
public class AjCommunityServiceImpl implements AjCommunityService {

    @Resource
    private AjCommunityMapper ajCommunityMapper;

    private static final String CODE_PREFIX = "COMMUNITY_";

    @Override
    public List<AjCommunityDto> queryList(AjCommunity ajCommunity) {
        return ajCommunityMapper.queryList(ajCommunity);
    }

    @Override
    public int insertAjCommunity(AjCommunity ajCommunity) {
        //设置小区编码
        ajCommunity.setCommunityCode(CODE_PREFIX+System.currentTimeMillis());
        return ajCommunityMapper.insert(ajCommunity);
    }

    @Override
    public AjCommunity selectAjCommunityById(Long communityId) {
        return ajCommunityMapper.selectById(communityId);
    }

    @Override
    public int updateAjCommunity(AjCommunity ajCommunity) {
        return ajCommunityMapper.updateById(ajCommunity);
    }

    @Override
    public int deleteAjCommunity(Long[] communityIds) {
        return ajCommunityMapper.deleteBatchIds(Arrays.asList(communityIds));
    }

    @Override
    public List<AjCommunityVo> queryPullDown(AjCommunity ajCommunity) {

        List<AjCommunityDto> dtoList = ajCommunityMapper.queryList(ajCommunity);

        //对象拷贝
        List<AjCommunityVo> voList = dtoList.stream().map(dto -> {

            //使用orik完成对象拷贝
            AjCommunityVo communityVo = OrikaUtils.convert(dto, AjCommunityVo.class);
            return communityVo;
        }).collect(Collectors.toList());

        return voList;
    }
}
